package com.hisence.service;

import com.hisence.dto.ResultDto;
import com.hisence.mapper.Cmpmapper;
import com.hisence.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class CmpService {
 @Autowired
    private Cmpmapper cmpmapper;

 @Transactional
    public ResultDto insertChuku(Yaohuo yaohuo) {
        ResultDto resultDto = new ResultDto();
        Chuku chuku = new Chuku();
        String uuid =UUID.randomUUID().toString();
        String querenid =UUID.randomUUID().toString();
        chuku.setId(uuid);
        chuku.setXiadanid(yaohuo.getBillno());
        chuku.setQuerenid(querenid);
        int i = cmpmapper.insertChuku(chuku);
        if (i<0){
            resultDto.setMessage("出库单生成失败");
            resultDto.setStatus(false);
        }
        List<Detil> detils = yaohuo.getDetils();
        for (Detil detil :detils){
            Cdetil cdetil = new Cdetil();
            Jiekoudetil jiekoudetil = new Jiekoudetil();
            jiekoudetil.setId(UUID.randomUUID().toString());
            jiekoudetil.setMasterid(querenid);
            jiekoudetil.setShopcode(detil.getShopcode());
            jiekoudetil.setShopname(detil.getShopname());
            jiekoudetil.setShopnum(detil.getShopnum());
            cdetil.setId(UUID.randomUUID().toString());
            cdetil.setMasterid(uuid);
            cdetil.setShopcode(detil.getShopcode());
            cdetil.setShopname(detil.getShopname());
            cdetil.setShopnum(detil.getShopnum());
            int i1 = cmpmapper.insertChukuxq(cdetil);
            int i3 = cmpmapper.insertJiekouxq(jiekoudetil);
            if (i1<0){
                resultDto.setMessage("出库详情生成失败");
                resultDto.setStatus(false);
            }
            if(i3<0){
                resultDto.setMessage("接口详情生成失败");
                resultDto.setStatus(false);
            }
        }
     Jiekou jiekou = new Jiekou();
        jiekou.setId(querenid);
     int i2 = cmpmapper.insertJiekou(jiekou);
     if (i2<0){
         resultDto.setMessage("接口表生成失败");
         resultDto.setStatus(false);
     }

     resultDto.setMessage("四张表生成成功");
     resultDto.setStatus(true);
     return resultDto;


 }
}
